#!/bin/bash

# Docker 启动脚本
# 用于快速启动智能体分享平台的 Docker 服务

set -e  # 遇到错误立即退出

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

# 检查 Docker 是否安装
check_docker() {
    print_message $BLUE "检查 Docker 环境..."
    
    if ! command -v docker &> /dev/null; then
        print_message $RED "错误: Docker 未安装，请先安装 Docker"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
        print_message $RED "错误: Docker Compose 未安装，请先安装 Docker Compose"
        exit 1
    fi
    
    print_message $GREEN "✓ Docker 环境检查通过"
}

# 检查环境配置文件
check_env_file() {
    print_message $BLUE "检查环境配置文件..."
    
    if [ ! -f ".env.docker" ]; then
        print_message $YELLOW "警告: .env.docker 文件不存在，将使用默认配置"
    else
        print_message $GREEN "✓ 环境配置文件存在"
    fi
}

# 构建 Docker 镜像
build_images() {
    print_message $BLUE "构建 Docker 镜像..."
    
    # 使用 docker compose 或 docker-compose
    if docker compose version &> /dev/null; then
        COMPOSE_CMD="docker compose"
    else
        COMPOSE_CMD="docker-compose"
    fi
    
    $COMPOSE_CMD build --no-cache
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✓ Docker 镜像构建成功"
    else
        print_message $RED "错误: Docker 镜像构建失败"
        exit 1
    fi
}

# 启动服务
start_services() {
    print_message $BLUE "启动 Docker 服务..."
    
    # 使用 docker compose 或 docker-compose
    if docker compose version &> /dev/null; then
        COMPOSE_CMD="docker compose"
    else
        COMPOSE_CMD="docker-compose"
    fi
    
    $COMPOSE_CMD up -d
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✓ Docker 服务启动成功"
        print_message $BLUE "服务访问地址:"
        print_message $GREEN "  前端: http://localhost:80"
        print_message $GREEN "  后端 API: http://localhost:3001"
    else
        print_message $RED "错误: Docker 服务启动失败"
        exit 1
    fi
}

# 显示服务状态
show_status() {
    print_message $BLUE "\n服务状态:"
    
    # 使用 docker compose 或 docker-compose
    if docker compose version &> /dev/null; then
        COMPOSE_CMD="docker compose"
    else
        COMPOSE_CMD="docker-compose"
    fi
    
    $COMPOSE_CMD ps
}

# 显示日志
show_logs() {
    print_message $BLUE "\n查看服务日志 (按 Ctrl+C 退出):"
    
    # 使用 docker compose 或 docker-compose
    if docker compose version &> /dev/null; then
        COMPOSE_CMD="docker compose"
    else
        COMPOSE_CMD="docker-compose"
    fi
    
    $COMPOSE_CMD logs -f
}

# 停止服务
stop_services() {
    print_message $BLUE "停止 Docker 服务..."
    
    # 使用 docker compose 或 docker-compose
    if docker compose version &> /dev/null; then
        COMPOSE_CMD="docker compose"
    else
        COMPOSE_CMD="docker-compose"
    fi
    
    $COMPOSE_CMD down
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✓ Docker 服务已停止"
    else
        print_message $RED "错误: Docker 服务停止失败"
        exit 1
    fi
}

# 清理资源
clean_resources() {
    print_message $BLUE "清理 Docker 资源..."
    
    # 使用 docker compose 或 docker-compose
    if docker compose version &> /dev/null; then
        COMPOSE_CMD="docker compose"
    else
        COMPOSE_CMD="docker-compose"
    fi
    
    $COMPOSE_CMD down -v --rmi all
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✓ Docker 资源清理完成"
    else
        print_message $RED "错误: Docker 资源清理失败"
        exit 1
    fi
}

# 显示帮助信息
show_help() {
    echo "智能体分享平台 Docker 启动脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  start     构建并启动服务 (默认)"
    echo "  stop      停止服务"
    echo "  restart   重启服务"
    echo "  status    显示服务状态"
    echo "  logs      查看服务日志"
    echo "  build     仅构建镜像"
    echo "  clean     停止服务并清理资源"
    echo "  help      显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 start    # 启动服务"
    echo "  $0 logs     # 查看日志"
    echo "  $0 clean    # 清理资源"
}

# 主函数
main() {
    local action=${1:-start}
    
    case $action in
        "start")
            check_docker
            check_env_file
            build_images
            start_services
            show_status
            ;;
        "stop")
            stop_services
            ;;
        "restart")
            stop_services
            sleep 2
            check_docker
            check_env_file
            start_services
            show_status
            ;;
        "status")
            show_status
            ;;
        "logs")
            show_logs
            ;;
        "build")
            check_docker
            build_images
            ;;
        "clean")
            clean_resources
            ;;
        "help")
            show_help
            ;;
        *)
            print_message $RED "错误: 未知选项 '$action'"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"